package com.example.myapplication;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;


import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

public class GoodsListVerAdapter extends RecyclerView.Adapter<GoodsListVerAdapter.ViewHolder> {

    private final ArrayList<JSONObject> arrayList;

    public GoodsListVerAdapter(ArrayList<JSONObject> arrayList) {

        this.arrayList = arrayList;

    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

        //在此处 写 每一个子项需要执行的操作
        JSONObject jsonObject = arrayList.get(position);
        try {
            //加载图片
            Glide.with(holder.mainImageView).load(jsonObject.getString("goods_image_url")).into(holder.mainImageView);
            holder.nameTextView.setText(jsonObject.getString("goods_name"));
            holder.descTextView.setText(jsonObject.getString("goods_jingle"));
            holder.priceTextView.setText("￥" + jsonObject.getString("goods_price"));
            holder.storeTextView.setText(jsonObject.getString("store_name"));
            if(jsonObject.getString("is_own_shop").equals("1")){
                holder.ownShopTextView.setVisibility(View.VISIBLE);
                holder.storeTextView.setVisibility(View.INVISIBLE);
            }else{
                holder.ownShopTextView.setVisibility(View.INVISIBLE);
                holder.storeTextView.setVisibility(View.VISIBLE);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup group, int viewType) {
        View view = LayoutInflater.from(group.getContext()).inflate(R.layout.item_goods_list_ver, group, false);
        return new ViewHolder(view);
    }

    @Override
    public int getItemCount() {

        return arrayList.size();

    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        private final AppCompatImageView mainImageView;
        private final AppCompatTextView nameTextView;
        private final AppCompatTextView descTextView;
        private final AppCompatTextView priceTextView;
        private final AppCompatTextView storeTextView;
        private final AppCompatTextView ownShopTextView;

        private ViewHolder(View view) {

            super(view);
            mainImageView = view.findViewById(R.id.mainImageView);
            nameTextView = view.findViewById(R.id.nameTextView);
            descTextView = view.findViewById(R.id.descTextView);
            priceTextView = view.findViewById(R.id.priceTextView);
            storeTextView = view.findViewById(R.id.storeTextView);
            ownShopTextView = view.findViewById(R.id.ownShopTextView);

        }

    }

}
